import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'
import { Indicator } from 'mint-ui'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    // 自定义的共享状态
    isTabbarShow: true,
    comingList: [],
    comingTotal: 0,
    comingPageNum: 1,
    comingLoading: false,
    filmHeaderStyle: ''
  },
  mutations: {
    /*
    mutation 是唯一修改状态的地方，定义的方法中第一个参数state是自动传的，第二个参数才是用户自己传的
  */
    HideMaizuoTabbar (state, data) {
      state.isTabbarShow = data
    },
    ShowMaizuoTabbar (state, data) {
      state.isTabbarShow = data
    },
    /*
            action 异步请求获取的数据要用过mutation 来更改state中的状态值 comingList
            */
    comingListMutation (state, data) {
      state.comingList = [...state.comingList, ...data]
    },
    comingTotalMutation (state, data) {
      state.comingTotal = data
    },
    comingPageNumMutation (state) {
      state.comingPageNum++
    },
    comingLoadingMutationf (state) {
      state.comingLoading = false
    },
    comingLoadingMutationt (state) {
      state.comingLoading = true
    },
    comingLoadingChangeStyle (state) {
      state.filmHeaderStyle = 'transform:translate3d(100%,0px,0px);width:50%'
    },
    newPlayingLoadingChangeStyle (state) {
      state.filmHeaderStyle = 'transform:translate3d(0%,0px,0px);width:50%'
    }
  },
  actions: {
    // 异步数据请求 在action做
    getComingListAction (store) {
      Indicator.open({
        text: '加载中...',
        spinnerType: 'triple-bounce'
      })
      axios({
        url: `https://m.maizuo.com/gateway?cityId=110100&pageNum=${store.state.comingPageNum}&pageSize=10&type=2&k=6150297`,
        headers: {
          'X-Client-Info': '{"a":"3000","ch":"1002","v":"5.0.4","e":"16239969753025057135722497","bc":"110100"}',
          'X-Host': 'mall.film-ticket.film.list'
        }
      }).then(res => {
        // console.log(res)
        store.commit('comingListMutation', res.data.data.films)
        store.commit('comingTotalMutation', res.data.data.total)
        store.commit('comingPageNumMutation')
        Indicator.close()
      })
    }
  },
  modules: {}
})
